package com.five.dao.impl.backstage;

import com.five.dao.backstage.QianTaiDao;
import com.five.entity.backstage.TChange;
import com.five.entity.backstage.TTuifang;
import com.five.entity.backstage.TXufang;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 王登峰
 * @Description:
 * @Date: 2020/12/2 14:26
 */
@Repository
public class QianTaiDaoImpl implements QianTaiDao {
    @Override
    //查询该类型所有的房间（所有状态）
    public List<Map> selectAllRoom() {
        String sql = "select * from(\n" +
                "select a.roomcode,a.status,a.typeid,b.name tname,b.yj,b.money,c.* from t_room a \n" +
                "left join t_type b on a.typeid = b.id \n" +
                "left join t_ruzhudetail c on a.roomcode = c.roomid \n" +
                "order by a.id,c.id desc LIMIT 100)aa\n" +
                "group by roomcode\n";
        return DBUtil.queryMap(sql);
    }
    //增加退房记录
    @Override
    public int addTuiFang(TTuifang tTuifang) {
        String sql ="insert into t_tuifang values(null,?,?,?,?,?,?,?)";
        return DBUtil.manage(sql,tTuifang.getTszjr(),tTuifang.getRuzhuid(),tTuifang.getOrderid()
                ,tTuifang.getOutdate(),tTuifang.getTsmoney(),tTuifang.getTsyj(),tTuifang.getComm());
    }
    //改变房间的状态为2打扫
    @Override
    public int changeStatus(String roomcode){
        String sql = "update t_room set status = 2 where roomcode = ?";
        return DBUtil.manage(sql,roomcode);
    }
    //改变房间的状态为0空闲
    @Override
    public int changeStatus2(String roomcode){
        String sql = "update t_room set status = 0 where roomcode = ?";
        return DBUtil.manage(sql,roomcode);
    }
    //改变房间的状态为1入住
    @Override
    public int changeStatus3(String roomcode){
        String sql = "update t_room set status = 1 where roomcode = ?";
        return DBUtil.manage(sql,roomcode);
    }
    //改变房间的状态为3换房的房间
    @Override
    public int changeStatus4(String roomcode){
        String sql = "update t_room set status = 3 where roomcode = ?";
        return DBUtil.manage(sql,roomcode);
    }
    //查询空闲的房间
    @Override
    public List<Map> selectByKY(Integer typeid){
        String sql = "select * from t_room where status =0 and typeid = ?";
        return DBUtil.queryMap(sql,typeid);
    }
    //增加换房记录表
    @Override
    public int addChangeRoom(TChange tChange){
        String sql = "insert into t_changeroom values(null,?,?,?,?)";
        return DBUtil.manage(sql,tChange.getRuzhuid(),tChange.getTime(),tChange.getOldroomid(),tChange.getNewroomid());
    }
    //查询换房记录表
    @Override
    public List<Map> selectChangeRoom(String ruzhuid){
        String sql = "select * from t_changeroom a left join t_ruzhudetail b on a.ruzhuid = b.ruzhuid where a.ruzhuid = ? order by a.id desc";
        return DBUtil.queryMap(sql,ruzhuid);
    }
    //换房时，改变入住详情单
    @Override
    public int changeRuzhuDetail(String newroom, String oldroom){
        String sql = "update t_ruzhudetail set roomid = ? where roomid = ?";
        return DBUtil.manage(sql,newroom,oldroom);
    }
    //计算换房的次数
    @Override
    public  int changCount(String ruzhuid){
        String sql = "select count(*) a from t_changeroom where ruzhuid = ?";
        List<Map> list = DBUtil.queryMap(sql,ruzhuid);
        String a = list.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }
    //查询该订单的房间有没有分配完
    @Override
    public int orderFenPei(String orderid){
        String sql = "select count(*)a from t_ruzhudetail where orderid = ?";
        List<Map> list = DBUtil.queryMap(sql,orderid);
        String a = list.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }
    //查询该订单的房间有没有都退完
    @Override
    public int orderTuiFang(String orderid){
        String sql = "select count(*)a from t_tuifang where orderid = ? and ruzhuid <>'' and ruzhuid is not null";
        List<Map> list = DBUtil.queryMap(sql,orderid);
        String a = list.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }
    //根据订单号查出该订单定了几间房
    @Override
    public int countByOrder(String orderid){
        String sql = "select count from t_order where orderid = ?";
        List<Map> list = DBUtil.queryMap(sql, orderid);
        String count = list.get(0).get("count") + "";
        int i = Integer.parseInt(count);
        return i;
    }
    @Override
    public int changeOrderStatus(String orderid){
        String sql = "update t_order set status = 2 where orderid = ?";
        return DBUtil.manage(sql,orderid);

    }
    //根据入住id改变入住详情单的状态 1 换房
    @Override
    public int changeruzhustatus(String ruzhuid){
        String sql = "update t_ruzhudetail set hfstatus = 1 where ruzhuid = ?";
        return DBUtil.manage(sql,ruzhuid);
    }
    //根据入住id改变入住详情单的状态 2 续房
    @Override
    public int changeruzhustatus1(String ruzhuid){
        String sql = "update t_ruzhudetail set xfstatus = 1 where ruzhuid = ?";
        return DBUtil.manage(sql,ruzhuid);
    }
    //增加续房表
    @Override
    public int addXuFang(TXufang tXufang){
        String sql = "insert into t_xufang values(null,?,?,?,?)";
        return DBUtil.manage(sql,tXufang.getRuzhuid(),tXufang.getBegintime(),tXufang.getEndtime(),tXufang.getXfprice());
    }
    //查看续房表
    @Override
    public List<Map> selectxufangfang(String ruzhuid){
        String sql = "select * from t_xufang a left join t_ruzhudetail b on a.ruzhuid = b.ruzhuid where a.ruzhuid = ?";
        return DBUtil.queryMap(sql,ruzhuid);
    }
    //查看这个订单有提取退房的信息
    @Override
    public List<Map> selectTiQianTui(String orderid){
        String sql = "select * from t_order a right join t_tuifang b on a.orderid = b.orderid where status = 0 and a.orderid = ?";
        return DBUtil.queryMap(sql,orderid);
    }
    //如果该订单有提前退房了，把istiqian状态改为1
    @Override
    public int isTiQian(String orderid){
        String sql = "update t_order set istiqian = 1 where orderid = ?";
        return DBUtil.manage(sql,orderid);
    }
    //查找如果提前退订的最晚时间
    @Override
    public String selectZuiWan(String orderid){
        String sql = "select * from (select * from t_tuifang where orderid = ? order by outdate desc limit 1000)aa group by orderid";
        List<Map> list = DBUtil.queryMap(sql, orderid);
        String outdate = list.get(0).get("outdate") + "";
        return outdate;
    }
    //看这个订单是否住过
    @Override
    public int isZhuGuo(String orderid){
        String sql = "select count(*) a from t_ruzhudetail where orderid = ?";
        List<Map> list = DBUtil.queryMap(sql, orderid);
        String a = list.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }
    @Override
    public int selectZhuGou(String orderid){
        String sql = "update t_order set iszhugou = 1 where orderid = ?";
        return DBUtil.manage(sql,orderid);
    }
    //改变订单的状态为 4 取消
    @Override
    public int changeOrderStatus4(String orderid){
        String sql = "update t_order set status = 4 where orderid = ?";
        return DBUtil.manage(sql,orderid);
    }
    //如果取消订单 把isquxiao改为1
    @Override
    public int changequxiao(String orderid){
        String sql = "update t_order set isquxiao = 1 where orderid = ?";
        return DBUtil.manage(sql,orderid);
    }
    //根据订单查找入住详情
    @Override
    public List<Map> selectRuzhu(String orderid){
        String sql = "select * from t_ruzhudetail where orderid =?";
        return DBUtil.queryMap(sql,orderid);
    }
}
